package com.durian.ui.span;

import android.content.Context;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;


public class SpanTextUtils {

    /**
     * 说明：设置多种自定义的文字
     *
     * @param settings
     */
    public static void setText(TextView tv, SpanSetting... settings) {
        if (settings.length > 0) {
            List<SpanSetting> list = new ArrayList<SpanSetting>();
            for (int i = 0; i < settings.length; i++) {
                list.add(settings[i]);
            }
            setText(tv, list);
        }
    }

    /**
     * 说明：设置多种自定义的文字
     *
     * @param tv
     * @param charSequences
     */
    public static void setText(TextView tv, List<SpanSetting> charSequences) {
        if (tv == null && charSequences == null) {
            return;
        }
        SpannableStringBuilder builder = new SpannableStringBuilder();
        if (check(charSequences)) {
            for (int i = 0; i < charSequences.size(); i++) {
                final SpanSetting setting = charSequences.get(i);
                if (setting == null){
                    continue;
                }
//                设置图片
                if (setting.getPic() > 0){
                    setting.setCharSequence("icon");
                    SpannableString spannableString = new SpannableString(
                            setting.getCharSequence());
                    VerticalImageSpan imgSpan = new VerticalImageSpan(tv.getContext(),setting.getPic(),setting.getScaleX(),setting.getScaleY());
                    spannableString.setSpan(imgSpan,0, setting.getCharSequence().length(),
                            Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                    builder.append(spannableString);
                    continue;
                }
//                设置文字
                if (setting.getCharSequence() == null
                        || TextUtils.isEmpty(setting.getCharSequence()
                        .toString())) {
                    continue;
                }
                SpannableString spannableString = new SpannableString(
                        setting.getCharSequence());
                // 设置字体大小
                float fontSize = setting.getFontSize();
                if (fontSize != 0.0f) {
                    fontSize = sp2px(tv.getContext(),fontSize);
                } else {
                    fontSize = tv.getTextSize();
                }
                // 设置属性
                if (setting.getColor() == Color.TRANSPARENT) {
                    setting.setColor(tv.getCurrentTextColor());
                }
                // 设置点击事件
                if (setting.getOnClickSpan() == null){
                    spannableString.setSpan(new NormalSpan(setting,(int) fontSize),
                            0,setting.getCharSequence().length(),Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                }else {
                    spannableString.setSpan(new CustomClickSpan(setting, i), 0,
                            setting.getCharSequence().length(),
                            Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                }
                builder.append(spannableString);
            }
        }
        if (builder.length() > 0) {
            tv.setHighlightColor(Color.TRANSPARENT);
//            tv.setClickable(false);
//            tv.setMovementMethod(LinkMovementClickMethod.getInstance());
            tv.setText(builder);
        }
    }

    /**
     * 根据手机的分辨率从 sp 的单位 转成为 px
     */
    private static int sp2px(Context context,float spValue) {
        float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    private static boolean check(List<SpanSetting> charSequences) {
        return !(charSequences == null || charSequences.isEmpty());
    }

}
